/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var searchInsert = function(nums, target) {
    let i = 0,
        j = nums.length;
    if(j===0){
        return 0;
    }
    if(target<nums[0])return 0;
    if(target>nums[j-1])return j;

    while(i<j){
        let m = Math.floor((i+j)/2);
        if(nums[m]===target)return m;
        if(i === j-1){
            if(nums[j]===target)return j;
            if(nums[i]<target&&nums[j]>target)return i+1;
            //return [i,j];
        }
        if(nums[m]<target){
            i = m;
        }
        if(nums[m]>target){
            j = m;
        }

    }
    return -1;
};


console.log(searchInsert([1,3,5,6,6],6));